package com.studyyang.workmodoul.workqueue;

import com.google.gson.Gson;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.studyyang.utils.RabbitmqUtils;
import com.studyyang.utils.constant.RabbitMqConstant;
import com.studyyang.workmodoul.workqueue.entity.OrderSms;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * 模拟:订单系统，异步调用短信服务发送消息
 * 作为消息的发送者
 *
 * @Author yangjin
 * @Description
 * @Date 2021/10/28/23:07
 **/
public class OrderSystem {

    public static void main(String[] args) throws IOException, TimeoutException {
        //获取mq连接
        Connection mqConn = RabbitmqUtils.getRabbitMqConn();
        //获取信道连接
        Channel channel = mqConn.createChannel();
        //声明队列
        channel.queueDeclare(RabbitMqConstant.WORK_QUEUE_ORDER_SMS,false,false,false,null);
        //发送消息
        for (int i = 0;i < 100;i++){
            OrderSms orderSms = new OrderSms("乘客【" + i + "】", "12345690" + i, "您的购票订单已生效");
            String jsonSms = new Gson().toJson(orderSms);
            //发送消息
            channel.basicPublish("",RabbitMqConstant.WORK_QUEUE_ORDER_SMS,null,jsonSms.getBytes(StandardCharsets.UTF_8));
            System.out.println("乘客"+i+"的消息发送成功!");
        }
        System.out.println("消息全部发送成功！");
        //关闭连接
        channel.close();
        mqConn.close();
    }
}
